<template>
	<view class="wrapper">
		<!-- <view class="authorized-btn" @click="authorizedAccess()"></view> -->
		<button class="authorized-btn" @click="accredit" v-if="isRegister">授权进入</button>
		<button class="authorized-btn" open-type="getPhoneNumber" @getphonenumber="getphonenumber" v-else>授权进入</button>
		
		<view class="safety-tips">
		    <view class="tips-top">
				<view class="user-handle" @click="handleSelect()">
					<image src="../../static/images/check-wx.png" class="is-check" v-if="pitchOn"></image>
					<image src="../../static/images/uncheck.png" class="is-check" v-else></image>
					已阅读并同意
				</view>
				<view class="user-text" @click="toPrivacy">《用户协议》</view>
			</view> 
		</view>
	</view>
</template>

<script>
	// import protoRoot from '@/proto/proto.js';
	// import {
	// 	connectSocket,
	// 	sendSocketMessage,
	// 	closeSocket
	// } from '@/util/socketManager.js';
	export default {
		data() {
			return {
				pitchOn: false,
				wxCode: '',
				isRegister: false,
				sceneid: '',
			}
		},
		onLoad(options) {
			if (options && options.q) { //微信扫一扫进入
				let scene = this.globalData.scene;
				let q = options.q;
				q = decodeURIComponent(q);
				let queryParams = getUrlParam(q) || {};
				console.log('queryParams', queryParams);
				this.sceneid = queryParams.sceneid;
				if(this.sceneid) {
					console.log('sceneid', this.sceneid);
					if (scene == 1011) { //扫码进入或微信主界面下拉
						if (!!!this.accessToken) { //未登录或首次登录
							//需要获取手机号，显示按钮
						} else { //已登录
							//无需手机号
							
						}
						
						//https://lhz.rigourtech.com:8091/rigourWeChatCallback/weixin/getwebchatcode?sceneid=liu_09e35ca3be824603a87d7d6bc4dd4786&jsCode=0c1oXRkl2LafIe4b5dml2v19MV2oXRk3
						//调用注册接口
					}
				}
			}
		},
		onShow() {
			uni.login({
				success: res => {
					console.log('微信code....', res.code);
				    this.wxCode = res.code;
					//this.sendProtobufMessage();
				}
			})
		},
		methods: {
			// sendProtobufMessage() {
			// 	let jsonObj = {
			// 		message: this.sceneid,
			// 	};
			// 	var jsonString = JSON.stringify(jsonObj);
			// 	// 准备消息内容
			// 	const messagePayload = {
			// 		type: 2,
			// 		idmessage: {
			// 			userid: '145676543212345',
			// 			username: 'asdfghjk'
			// 		},
			// 	};
				
			// 	// 验证消息结构
			// 	const errMsg = protoRoot.WrapperMessage.verify(messagePayload);
			// 	if (errMsg) throw Error(errMsg);
				
			// 	// 创建消息类型
			// 	let messageObj = protoRoot.WrapperMessage.create(messagePayload);
				
			// 	//encode数据
			// 	let messageObjBuffer = protoRoot.WrapperMessage.encode(messageObj).finish(); 
				
			// 	connectSocket((res) => {
			// 		if(res == 'already connected'){
			// 			return;
			// 		}
			// 		sendSocketMessage(messageObjBuffer, (res) => {
			// 			// closeSocket();
			// 			//发送成功失败与否都需要跳转到首页
			// 			uni.switchTab({
			// 				url: '/pages/dataStatistics/index'
			// 			})
			// 		}); 
			// 	});
			// },
			//以下关于微信登录部分
			getphonenumber(e) {
				// let detail = e.mp.detail;
				console.log(e);
				return;
				if(detail.encryptedData && detail.iv){ //允许
					if(this.wxCode) { //onshow中已获取
						let data = {
							jsCode: this.wxcode,
							sceneid: this.sceneid,
							encryptedData: detail.encryptedData,
							iv: detail.iv
						}
						//uni.showLoading({title:'加载中'});
						// wxKeyLogin(data).then(res =>{
						// 	uni.hideLoading();
						// 	if(res.code == 200){
						// 		let data = res.data
						// 		this.globalData.userInfo = data
						// 		this.globalData.token = data.token
						// 		uni.setStorageSync('userInfo', data)
						// 		uni.setStorageSync('token',  data.token)
						// 		uni.setStorageSync('userId', data.userId)
						// 		if (data.veriftyStatus == 1 && data.certStatus == 1) { //1:未认证,2:身份证通过认证,3:人脸对比通过认证
						// 			uni.reLaunch({
						// 				url:'../../pagesB/realName/index'
						// 			})
						// 		} else {
						// 			// #ifdef MP-WEIXIN
						// 			uni.navigateTo({
						// 				url:'../../pages/home/homePage/index'
						// 			})
						// 			// #endif
						// 			// #ifdef MP-ALIPAY
						// 			uni.reLaunch({
						// 				url:'../../pages/home/homePage/index'
						// 			})
						// 			// #endif
						// 		}
						// 	}
						// }).catch(err =>{
						// 	uni.hideLoading()
						// })
					}else { //由于wx.login没有及时返回code，做出提示
						uni.showToast({
							title: '似乎出了点问题，请再次点击',
							icon: 'none'
						})
					}
				}else{ //拒绝
				
				}
			},
			accredit() {
				
			},
			authorizedAccess() {
				wx.getFuzzyLocation({
					type: 'wgs84',
					success (res) {
						const latitude = res.latitude;
						const longitude = res.longitude;
						console.log(res);
					}
				});
			},
			toPrivacy(){
				uni.navigateTo({url:'rules/index'})
			},
			handleSelect(){
				this.pitchOn = !this.pitchOn
			},
		}
	}
</script>

<style scoped lang="scss">
	.wrapper{
		.authorized-btn{
			margin: 300upx auto 150upx;
			width: 80vw;
			background: $main-color;
			display: flex;
			justify-content: center;
			align-items: center;
			border-radius: 20upx;
			color: #fff;
			height: 70upx;
			line-height: 70upx;
		}
		.safety-tips{
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			font-size: 30upx;
			width: 100%;
			color: #282828;
			.tips-top{
				width: 100%;
				display: flex;
				justify-content: center;
				align-items: center;
				margin-bottom: 10upx;
				.user-handle{
					display: flex;
					align-items: center;
					.is-check{
						width: 37upx;
						height: 37upx;
						margin-right: 10upx;
					}
				}
				.user-text{
					color: #EBAB51;
				}
			}
		}
	}

</style>
